package com.wakey.rtcs.utils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

public class FileUpTool {
	
	
	/**
	 * 上传附件
	 * @param request
	 * @param folderName 存贮文件夹名称
	 * @param fileMaxSize 	文件大小
	 * @param file		附件
	 * @return String 文件存储后名称
	 */
	
	public static String uploadFileTool (HttpServletRequest request,String folderName,String fileMaxSize,MultipartFile file){
		String recName = null;
		 if (!file.isEmpty()) {
	        	String originalFileName = file.getOriginalFilename();
	        	String realFileName = UUID.randomUUID().toString()+originalFileName.substring(originalFileName.lastIndexOf("."));
	        	Path dir = getFileDir(request,folderName);
	        	Path target = dir.resolve(realFileName);
	        	long filesize = file.getSize();
	    		long m = filesize/(1024*1024);
	        	try {
	        		System.out.println(target);
	        		if(m<Long.parseLong(fileMaxSize)){
		        		if(!Files.exists(dir)) {
		        			Files.createDirectories(dir);
		        		}
		        		recName= realFileName ;
						Files.copy(file.getInputStream(), target);
	        		}else{
	        			recName="error";
	        		}
				} catch (IOException e) {
					recName="error";
					e.printStackTrace();
				}
	        }
		 return  recName;
	}
	/**
	 * 获取实际存储文件的目录
	 * @param request
	 * @return
	 */
	public static Path getFileDir(HttpServletRequest request,String folderName) {
		System.out.println("=====================");
		System.out.println(folderName);
		//获得项目路径
		String realPath = request.getSession().getServletContext().getRealPath("/");
		System.out.println(realPath);
		//获得工程名称
		String realPath1 = request.getContextPath();  
		System.out.println(realPath);
    	//将附件发布移除到与工程同层级
		Path dir = Paths.get(realPath.substring(0, realPath.length()-realPath1.length())).resolve(folderName);
		return dir;
	}
	/**
	 * 上传附件
	 * @param request
	 * @param folderName 存贮文件夹名称
	 * @param fileMaxSize 	文件大小
	 * @param file		附件
	 * @return String 文件存储后名称
	 */
	
	public String uploadFileToolTwo (HttpServletRequest request,String folderName,String fileMaxSize,MultipartFile file){
		String recName = null;
		 if (!file.isEmpty()) {
	        	String originalFileName = file.getOriginalFilename();
	        	//String realFileName = UUID.randomUUID().toString()+originalFileName.substring(originalFileName.lastIndexOf("."));
	        	String realFileName = originalFileName;
	        	Path dir = getFileDir(request,folderName);
	        	Path target = dir.resolve(realFileName);
	        	long filesize = file.getSize();
	    		long m = filesize/(1024*1024);
	        	try {
	        		//System.out.println(target);
	        		if(m<Long.parseLong(fileMaxSize)){
		        		if(!Files.exists(dir)) {
		        			Files.createDirectories(dir);
		        		}
		        		recName= realFileName ;
		        		//检查上传文件是否存在存在就不上传了
		        		File firstUsed = new File(target+"");
		        		if (!firstUsed.exists()){  
		        			Files.copy(file.getInputStream(), target);
		        		}
	        		}else{
	        			recName="error";
	        		}
				} catch (IOException e) {
					recName="error";
					e.printStackTrace();
				}
	        }
		 return  recName;
	}

}
